package com.shen.testMysql;

import java.sql.*;
public class MysqlConfig {

    public static void main(String[] args) {

        //与数据库建立连接
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");  // 加载驱动
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动失败");
            System.out.println(e);
        }
        String url = "jdbc:mysql://localhost:3306/" + "test20230521";
        String user = "root";
        String pwd = "shx336699";
        Connection con = null;

        try {
            con = DriverManager.getConnection(url, user, pwd);
            Statement state = con.createStatement();
            // sql语句的编写规则：java中的字符串只能双引号，如果字符串中需要拼接变量，该变量用单引号括起来
            String sql = " SELECT * FROM t_test_people";
            ResultSet rs = state.executeQuery(sql);  // 执行查询语句
            System.out.println("---------------1------------");
            while (rs.next()){
                int id = rs.getInt("id");                // 获取 id 值
                String name = rs.getString("name");      // 获取 name 值
                String age = rs.getString("age");  // 获取 age 值
                System.out.println("id:" + id + ", name:" + name + ", age:" + age);
            }
            System.out.println("---------------2------------");
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
